import { Init, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm';
import { FlockInfoEntity } from '../entity/info';
import { FlockInfoTypeEntity } from '../entity/infoType';
import { FlockUserEntity } from "../../flock_user/entity/user"
import { FlockCommentEntity } from "../entity/comment"

/**
 * flock模块-群信息
 */
@Provide()
export class FlockInfoTypeService extends BaseService {
  @InjectEntityModel(FlockInfoEntity)
  flockInfoEntity: Repository<FlockInfoEntity>;

  @InjectEntityModel(FlockInfoTypeEntity)
  flockInfoTypeEntity: Repository<FlockInfoTypeEntity>;

  @InjectEntityModel(FlockUserEntity)
  flockUserEntity: Repository<FlockUserEntity>;

  @InjectEntityModel(FlockCommentEntity)
  flockCommentEntity: Repository<FlockCommentEntity>;

  @Init()
  async init() {
    await super.init();
    this.setEntity(this.flockInfoEntity);
  }

  async getFlockDetail(queryData: any) {
    let detail: any = await this.flockInfoEntity.findOneBy({ id: queryData.goodsId })
    let comments = detail.comments
    let members = detail.members.map( o =>Number(o))
    let membersList = await this.flockInfoTypeEntity.findBy({ userCommentId: In(members), flockType: 0 })
    let commentsList = await this.flockInfoTypeEntity.findBy({ userCommentId: In(comments), flockType: 1 })
    let membersListData = await this.flockUserEntity.findBy({ id: In(membersList.map(o => o.userCommentId)) })
    let commentsListData = await this.flockCommentEntity.findBy({ id: In(commentsList.map(o => o.userCommentId)) })
    detail.comments = commentsListData
    detail.members = membersListData
    delete detail.flockQrCode
    delete detail.flockQrCodeNumber
    return detail
  }

}
